<?php
$this->Html->addCrumb('Contents', '/admin/contents');
$this->Html->addCrumb('Edit');
//CSS - Markitup
echo $this->Html->css(array('markitup/skins/markitup/style','markitup/sets/html/style'), false);
//Javascript
//WYM Editor
echo $this->Html->script(array('plugins/wymeditor/jquery.wymeditor','plugins/wymeditor/plugins/resizable/jquery.wymeditor.resizable','plugins/wymeditor/plugins/hovertools/jquery.wymeditor.hovertools'), false);
//Markitup
echo $this->Html->script(array('plugins/jquery.markitup-1.1.10','/css/markitup/sets/html/set'), false);

$pageContent=addslashes($contents["Content"]["info"]);
$pageContentLean = preg_replace( '/\r\n/', ' ', trim($pageContent) );
$markitupPreviewUrl ="/". APP_DIR."/".WEBROOT_DIR."/".CSS_URL."markitup/preview/preview.php?css=".APP_DIR."/".WEBROOT_DIR."/".CSS_URL."style.css";
$this->Html->scriptStart(array('inline' => false));
echo '
      $(function(){
        $("#ContentAdminEditForm").TWS_Form( {
          validate : {
            rules : {ContentInfoDisplay : { help : "This is the information that will be displayed on the page"}}
          }
        });

        $("#textAreaContainer").hide();
        $("#textAreaBasic").hide();
        $("#textAreaBasicDisplay").removeClass("hide").show();
        WYMeditor.PREVIEW_CSS = $("link#previewCss").attr("href");
        WYMeditor.FRAMEWORK_CSS = $("link#frameworkCss").attr("href");
        var editor = $(".wymeditor").wymeditor({
          html: "<p>Coming soon...<\/p>",
          logoHtml : "",
          //stylesheet : "/css/wymeditor.css",
          previewCss : "/css/style.css",
          formPluginPath : "'.Configure::read('App.contentFormPluginPath').'",
          uploadImagePath : "'.Configure::read('App.contentUploadImagePath').'",
          postInit: function(wym) {
            $(wym._box).find(wym._options.containersSelector)
            .removeClass("wym_dropdown")
            .addClass("wym_panel")
            .find("h2 > span")
            .remove();
            wym.hovertools();
            //This line of code removes the classes container
            $(wym._box).find("div.wym_classes").remove();
            $("#ContentInfo").val("");            
            $("#textAreaContainer").show().find("label").css("float","none");
            $.wymeditors(0).html("'.$pageContentLean.'");
          }
          });

          $("#ContentInfoAdvancedDisplay").markItUp($.extend(mySettings, {previewTemplatePath: "'.$markitupPreviewUrl.'"}));
          $("#ContentInfoAdvancedDisplay").val("'.$pageContentLean.'");

          $("#editBtn").click(function() {
          var result = "";
          if ($("#textAreaBasicDisplay").is(":visible"))  {
            result = $.wymeditors(0).xhtml();
            } else {
              result = $("#ContentInfoAdvancedDisplay").val();
            }
            result = result.replace(/[\r\n]+/g, "");
            $("#ContentInfo").val(result);
            return true;
          });

          //Editors
          $("#editorSelections").show();
          $("#basicEditor").hide();
          $("#advancedEditor").click(function() {
            $("#basicEditor").show();
            $("#advancedEditor").hide();
            $("#ContentInfoAdvancedDisplay").val($.wymeditors(0).xhtml());
            $("#textAreaBasicDisplay").hide();
            $("#textAreaAdvancedDisplay").show();
            return false;
          });
          $("#basicEditor").click(function() {
            $("#basicEditor").hide();
            $("#advancedEditor").show();
            $.wymeditors(0).html($("#ContentInfoAdvancedDisplay").val());
            $("#textAreaBasicDisplay").show();
            $("#textAreaAdvancedDisplay").hide();
            return false;
          });
        });
       ';
$this->Html->scriptEnd();
?>

<h1><?php echo $contents["Content"]["content_description"];?></h1>
<?php echo $this->Session->flash();?>
<?php echo '<div id="actionLink">'.$this->Html->link('Back to index',array('action'=>'index')).'</div>'; ?>
<div id="editorSelections" class="hide"><div><a id="advancedEditor" href="#advancedEditor">Switch to advanced HTML editor</a></div><div><a  id="basicEditor" href="#basicEditor">Switch to basic editor</a></div></div>
<?php echo $this->element('errors', array('errors' => isset($errors)? $errors : null)); ?>
<?php
echo $this->TWSForm->create('Content',  array(
    'inputDefaults' => array(
        'div' => array('class'=>'row')),
        'class'=>'inlineForm',
        'type'=>'file'));
echo '<div id="textAreaContainer">';
    echo '<div id="textAreaBasic">';
      echo $this->TWSForm->input('info', array("rows"=>"15", "cols"=>"125", "name"=>"ContentInfo", 'label'=>"Content for page","value"=>$contents["Content"]["info"]));
    echo '</div>';
    echo '<div id="textAreaBasicDisplay" class="hide">';
      echo $this->TWSForm->input('info_display', array('rows'=>'5','name'=>'ContentInfoDisplay', 'label'=>"Content for page", 'class'=>"wymeditor"));
    echo '</div>';
    echo '<div id="textAreaAdvancedDisplay" class="hide" >';
      echo $this->TWSForm->input('info_advanced_display', array('rows'=>'5','name'=>'ContentInfoAdvancedDisplay', 'label'=>"Content for page"));
    echo '</div>';
echo '</div>';
echo $this->TWSForm->submit('Update', array('id'=>'editBtn', 'name'=>'editBtn', 'class'=>'button', 'div'=>false));
echo $this->TWSForm->end();
?>